﻿/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Windows;
using SilverChart.Configuration;
using SilverChart.Core.Log;

namespace SilverChart.Components {
    public class CartesianCoordinateComposite : CompositeChartWidget, RelativeClientProvider {
        private static Logger logger = Logger.GetLogger<CartesianCoordinateComposite>();

        private XAxisComponent xAxis;
        private XAxisGridComponent xAxisGrid;

        private YAxisComponent yAxis;
        private YAxisGridComponent yAxisGrid;

        public CartesianCoordinateComposite() {
            WidgetsManager.AddComponent(this);

            //Background = BrushHelper.GetSolidColorBrush("Blue");

            xAxis = new XAxisComponent(Configurations.Instance.XAxis);
            yAxis = new YAxisComponent(Configurations.Instance.YAxis);
            Children.Add(xAxis);
            Children.Add(yAxis);

            if (xAxis.AxisConfig.MajorAxisGrid || xAxis.AxisConfig.MinorAxisGrid) {
                xAxisGrid = new XAxisGridComponent(xAxis);
                Children.Add(xAxisGrid);
            }

            if (yAxis.AxisConfig.MinorAxisGrid || yAxis.AxisConfig.MajorAxisGrid) {
                yAxisGrid = new YAxisGridComponent(yAxis);
                Children.Add(yAxisGrid);
            }
        }

        protected override Size MeasureOverride(Size availableSize) {
            xAxis.Measure(availableSize);
            Double firstHeight = xAxis.DesiredSize.Height;
            yAxis.Measure(new Size(availableSize.Width, availableSize.Height - firstHeight));
            xAxis.Measure(new Size(availableSize.Width - yAxis.DesiredSize.Width, availableSize.Height));
            if (Math.Abs(xAxis.DesiredSize.Height - firstHeight) > 0E-10) {
                yAxis.Measure(new Size(availableSize.Width,
                    availableSize.Height - xAxis.DesiredSize.Height));
            }

            Size clientSize = new Size(xAxis.DesiredSize.Width, yAxis.DesiredSize.Height);
            if (xAxis.AxisConfig.MajorAxisGrid || xAxis.AxisConfig.MinorAxisGrid) {
                xAxisGrid.Measure(clientSize);
            }

            if (yAxis.AxisConfig.MajorAxisGrid || yAxis.AxisConfig.MinorAxisGrid) {
                yAxisGrid.Measure(clientSize);
            }

            Size allChildSize = new Size(xAxis.DesiredSize.Width + yAxis.DesiredSize.Width,
                xAxis.DesiredSize.Height + yAxis.DesiredSize.Height);
            logger.Debug("Measured Available Size:" + allChildSize.ToString());

            return allChildSize;
        }

        protected override Size ArrangeOverride(Size finalSize) {
            Rect xAxisRect = new Rect(yAxis.DesiredSize.Width, finalSize.Height - xAxis.DesiredSize.Height,
                finalSize.Width - yAxis.DesiredSize.Width, xAxis.DesiredSize.Height);
            xAxis.Arrange(xAxisRect);
            Rect yAxisRect = new Rect(0, 0, yAxis.DesiredSize.Width,
                finalSize.Height - xAxis.DesiredSize.Height);
            yAxis.Arrange(yAxisRect);

            Rect gridRect = new Rect(yAxis.DesiredSize.Width, 0, xAxis.DesiredSize.Width, yAxis.DesiredSize.Height);
            if (xAxis.AxisConfig.MajorAxisGrid || xAxis.AxisConfig.MinorAxisGrid) {
                xAxisGrid.Arrange(gridRect);
            }

            if (yAxis.AxisConfig.MajorAxisGrid || yAxis.AxisConfig.MinorAxisGrid) {
                yAxisGrid.Arrange(gridRect);
            }
            return finalSize;
        }

        public override string WidgetName {
            get {
                return "Cartesian Coordinate System Composite";
            }
        }

        #region RelativeClientProvider Members

        public Rect GetRelativeClientArea(Rect orginalClientRect, RelativeClientConsumer consumer) {
            Rect clientArea = new Rect(yAxis.DesiredSize.Width, 0,
                xAxis.DesiredSize.Width, yAxis.DesiredSize.Height);
            return clientArea;
        }

        public Size GetRelativeClientSize(Size orginalCilentSize, RelativeClientConsumer consumer) {
            Size clientSize = new Size(xAxis.DesiredSize.Width, yAxis.DesiredSize.Height);
            return clientSize;
        }

        #endregion
    }
}
